package com.tinghua.mqTest.exchanges;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;
import com.tinghua.mqTest.utils.ConnectionMQ;

import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * 交换机的配置与绑定
 */
public class ExchangeTest {
    public static final String EXCHANGE_NAME = "logsFile";
    public static void main(String[] args) throws Exception{
        //生产者
        //producerTest();
        //消费者
        consumerTest();

    }
    public static void consumerTest() throws Exception{
        Channel channel = ConnectionMQ.utilConnectionMQ();
        String queue = channel.queueDeclare().getQueue();
        channel.queueBind(queue, EXCHANGE_NAME, "");
        DeliverCallback deliverCallback = (String consumerTag, Delivery message) -> {
            System.out.println("A接收到消息:" + new String(message.getBody()));
        };
        channel.basicConsume(queue, deliverCallback, (String consumerTag) -> {});
    }
    public static void producerTest() throws Exception{
        Channel channel = ConnectionMQ.utilConnectionMQ();
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            String msg = sc.next();
            channel.basicPublish(EXCHANGE_NAME, "", null, msg.getBytes(StandardCharsets.UTF_8));
        }

    }
}
